package vfs.service.impl;

import java.sql.Connection;
import java.util.Iterator;
import vfs.except.VFSEntityException;
import vfs.filter.VFSFilter;
import vfs.filter.impl.VFSFilterImpl;
import vfs.obj.entity.VFSEntityFactory;
import vfs.obj.entity.VFSNodeType;
import vfs.obj.entity.VFSUser;
import vfs.obj.entity.impl.VFSEntityFactoryImpl;
import vfs.service.VFSNodeTypeService;

/**
 *
 * @author Matthew Eavenson <durandal@uga.edu>
 */
public class VFSNodeTypeServiceImpl implements VFSNodeTypeService{

    private VFSUser user;
    private VFSEntityFactory eFactory;
    
    public VFSNodeTypeServiceImpl(Connection conn, VFSUser user){
        if(user == null) throw new VFSEntityException("Invalid user");
        this.user = user;
        eFactory = new VFSEntityFactoryImpl(conn);
    }
    
    @Override
    public VFSNodeType getNodeType(String name) {
        VFSFilter filter = new VFSFilterImpl();
        filter.addConstraint("name", name, VFSFilter.VFSFilterOperator.EQ);
        Iterator<VFSNodeType> types = getNodeTypes(filter);
        if(types.hasNext()) return types.next();
        return null;
    }

    @Override
    public Iterator<VFSNodeType> getNodeTypes(VFSFilter filter) {
        return eFactory.findNodeTypes(filter);
    }

    @Override
    public boolean exists(String name) {
        return getNodeType(name) != null;
    }

    @Override
    public VFSNodeType create(String name, String description) {
        return eFactory.createNodeType(name, description);
    }
    
    @Override
    public boolean rename(VFSNodeType nodeType, String name){
        nodeType.setName(name);
        eFactory.storeNodeType(nodeType);
        return true;
    }

    @Override
    public boolean remove(String name) {
        VFSNodeType nodeType = getNodeType(name);
        if(nodeType != null){
            return remove(nodeType);
        }
        return false;
    }

    @Override
    public boolean remove(VFSNodeType nodeType) {
        eFactory.removeNodeType(nodeType);
        return true;
    }
    
}
